<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->
    <input type="file" @change="fileChange" hidden ref="userimg" />

    <!-- 个人信息 -->
    <van-cell
      class="avatar-cell"
      @click="chooseImg"
      title="头像"
      is-link
      center
    >
      <van-image class="avatar" round fit="cover" :src="userinfo.photo" />
    </van-cell>
    <van-cell
      title="昵称"
      @click="isEditNameShow = true"
      :value="userinfo.name"
      is-link
    />
    <van-cell
      title="性别"
      @click="isEditGenderShow=true"
      :value="userinfo.gender === 0 ? '男' : '女'"
      is-link
    />
    <van-cell
      title="生日"
      @click="isEditBirthdayShow=true"
      :value="userinfo.birthday ? userinfo.birthday : '无'"
      is-link
    />
    <!-- /个人信息 -->
    <!-- ------------------------------------------------------------------------------------- -->

    <!-- 编辑头像弹出层 -->
    <van-popup
      v-model="isEditPhotoShow"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <update-photo
        :imgUrl="previewImgUrl"
        v-if="isEditPhotoShow"
        @close="isEditPhotoShow = false"
        @updatePhoto="onUpdatePhoto"
      />
    </van-popup>

    <!-- /编辑头像弹出层 -->

    <!-- ------------------------------------------------------------------------------------- -->
    <!-- 编辑昵称弹出层 -->
    <van-popup
      v-model="isEditNameShow"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <update-name
        v-if="isEditNameShow"
        @close="isEditNameShow = false"
        v-model="userinfo.name"
      />
    </van-popup>
    <!-- /编辑昵称弹出层 -->

    <!-- ------------------------------------------------------------------------------------- -->

    <!-- 编辑性别弹出层 -->
    <van-popup
      v-model="isEditGenderShow"
      position="bottom"
      :style="{ height: '40%' }"
    >
      <update-gender
        v-if="isEditGenderShow"
        @close="isEditGenderShow = false"
        v-model="userinfo.gender"
      />
    </van-popup>
    <!-- /编辑性别弹出层 -->

    <!-- ------------------------------------------------------------------------------------- -->
   <!-- 编辑生日弹出层 -->
    <van-popup
      v-model="isEditBirthdayShow"
      position="bottom"
      :style="{ height: '40%' }"
    >
      <update-birthday
        v-if="isEditBirthdayShow"
        @close="isEditBirthdayShow = false"
        v-model="userinfo.birthday"
      />
    </van-popup>
    <!-- /编辑生日弹出层 -->

    <!-- ------------------------------------------------------------------------------------- -->
  </div>
</template>

<script>
import { getUserInfoDetail } from "@/api/user";
import UpdatePhoto from "./components/update-photo";
import UpdateName from "./components/update-name";
import UpdateGender from "./components/update-gender";
import UpdateBirthday from "./components/update-birthday";
export default {
  name: "UserProfile",
  components: {
    UpdatePhoto,
    UpdateName,
    UpdateGender,
    UpdateBirthday
  },
  props: {},
  data() {
    return {
      userinfo: {}, //用户资料
      isEditPhotoShow: false, // 头像弹框显示隐藏
      previewImgUrl: "",
      isEditNameShow: false,
      isEditGenderShow:false,
      isEditBirthdayShow:false
    };
  },
  computed: {},
  watch: {},
  created() {
    this.loadUserInfo();
  },
  mounted() {},
  methods: {
    async loadUserInfo() {
      try {
        const { data: res } = await getUserInfoDetail();
        this.userinfo = res.data;
      } catch (error) {
        this.$toast.fail("获取用户信息失败");
      }
    },
    // 点击弹出file
    chooseImg() {
      this.$refs.userimg.click();
    },
    fileChange() {
      // 文件信息
      // console.log(this.$refs.userimg.files[0]);
      // 生成预览地址
      // console.log(URL.createObjectURL(this.$refs.userimg.files[0]));
      const fileImgs = this.$refs.userimg.files;
      if (fileImgs.length) {
        this.previewImgUrl = URL.createObjectURL(fileImgs[0]);
      }
      this.isEditPhotoShow = true;
    },
    onUpdatePhoto(val) {
      this.userinfo.photo = val;
      this.isEditPhotoShow = false;
    }
  }
};
</script>

<style scoped lang="less">
.user-profile {
  .avatar-cell {
    .van-cell__value {
      display: flex;
      flex-direction: row-reverse;
    }
    .avatar {
      width: 60px;
      height: 60px;
    }
  }
}
</style>
